import random
import math
import json
from django.core.cache import cache
from django.shortcuts import render
from django.http.response import HttpResponse as response
from models.models import StoreMenu, StoreGoods


# 跳转到首页
def index(request):
    return render(request, "index.html")


# 跳转分类页
def to_classification(request):
    # 获取分页信息
    page_number = request.GET.get("page_number")
    if page_number is None:
        page_number = 1
    else:
        page_number = int(page_number)
    page_size = 15
    limit = (page_number - 1) * page_size
    offset = limit + page_size
    res = dict()
    menu_id = request.GET.get("menu_id")
    if menu_id is not None:
        menu = StoreMenu.objects.filter(id=menu_id)

        # 分页获取分类下得商品

        count = StoreGoods.objects.filter(type_id=menu_id).count()
        goods = StoreGoods.objects.filter(type_id=menu_id).order_by("-id")
        page_total = math.ceil(count / page_size)
        res_goods = []
        for g in goods:
            res_g = dict()
            res_g["id"] = g.id
            res_g["goods_title"] = g.goods_title
            res_g["goods_img"] = g.goods_img
            res_g["goods_price"] = \
                min(g.goods_wph_price, g.goods_jd_price, g.goods_tm_price, g.goods_tb_price, g.goods_sn_price)
            res_goods.append(res_g)

        # 排序
        sort = request.GET.get("sort")
        if sort is None:
            sort = 0
        else:
            sort = int(sort)
        if sort == 1:
            for i in range(len(res_goods)):
                for j in range(0,len(res_goods)-i-1):
                    if res_goods[j].get("goods_price") > res_goods[j+1].get("goods_price"):
                        res_goods[j], res_goods[j+1] = res_goods[j+1], res_goods[j]
        if sort == 2:
            for i in range(len(res_goods)):
                for j in range(0,len(res_goods)-i-1):
                    if res_goods[j].get("goods_price") < res_goods[j+1].get("goods_price"):
                        res_goods[j], res_goods[j+1] = res_goods[j+1], res_goods[j]

        res["menu_name"] = menu[0].menu_name
        res["goodsList"] = res_goods[limit:offset]
        res["page_total"] = page_total
        res["count"] = count
        res["page_number"] = page_number
        res["sort"] = sort
    keyword = request.GET.get("keyword")
    if keyword is not None:
        count = StoreGoods.objects.filter(goods_keyword__icontains=keyword).count()
        goods = StoreGoods.objects.filter(goods_keyword__icontains=keyword).order_by("-id")[limit:offset]
        page_total = math.ceil(count / page_size)
        res_goods = []
        for g in goods:
            res_g = dict()
            res_g["id"] = g.id
            on = "<span style='color:red;'>"
            off = "</span>"
            res_g["goods_title"] = g.goods_title
            res_g["goods_img"] = g.goods_img
            res_g["goods_price"] = \
                min(g.goods_wph_price, g.goods_jd_price, g.goods_tm_price, g.goods_tb_price, g.goods_sn_price)
            res_goods.append(res_g)
        res["keyword"] = keyword
        res["goodsList"] = res_goods[limit:offset]
        res["page_total"] = page_total
        res["count"] = count
        res["page_number"] = page_number
    return render(request, "classification.html", res)


# 跳转到商品详情
def to_details(request):
    goods_id = request.GET.get("goods_id")
    user = request.session.get("user")
    is_collection = 0
    if user is not None:
        # 用户登录，添加足迹信息
        ids = cache.get("browsing_history_" + str(user.get("id")))
        if ids is None:
            ids = dict()
        if ids.get(goods_id) is None:
            ids[goods_id] = 0
        ids[goods_id] += 1
        cache.set("browsing_history_" + str(user.get("id")), ids, None)
        # 获取收藏数据
        collections = cache.get("collection_" + str(user.get("id")))
        if collections is None:
            collections = list()
        if goods_id in collections:
            is_collection = 1
    goods = StoreGoods.objects.filter(id=goods_id).first()
    menu = StoreMenu.objects.filter(id=goods.type_id).first()
    res = dict()
    res["menu_name"] = menu.menu_name
    res["is_collection"] = is_collection
    res["good_id"] = goods_id
    res["goods_title"] = goods.goods_title
    res["goods_img"] = goods.goods_img
    res["goods_specification"] = goods.goods_specification
    res["goods_wph_price"] = goods.goods_wph_price
    res["goods_wph_link"] = goods.goods_wph_link
    res["goods_jd_price"] = goods.goods_jd_price
    res["goods_jd_link"] = goods.goods_jd_link
    res["goods_tm_price"] = goods.goods_tm_price
    res["goods_tm_link"] = goods.goods_tm_link
    res["goods_tb_price"] = goods.goods_tb_price
    res["goods_tb_link"] = goods.goods_tb_link
    res["goods_sn_price"] = goods.goods_sn_price
    res["goods_sn_link"] = goods.goods_sn_link

    res["goods_price"] = \
        min(goods.goods_wph_price, goods.goods_jd_price, goods.goods_tm_price, goods.goods_tb_price, goods.goods_sn_price)
    return render(request, "details.html", res)


# 跳转登录页面
def to_login(request):
    code = ""
    for i in range(6):
        code += str(random.randint(0, 9))
    res = dict()
    res["code"] = code
    return render(request, "login.html", res)


# 跳转注册页面
def to_reg(request):
    return render(request, "reg.html")


# 跳转到找回密码
def to_reset_password(request):
    return render(request, "resetPassword.html")


# 跳转到个人中心
def to_personal(request):
    return render(request, "personal.html")


# 跳转到个人资料
def to_information(request):
    return render(request, "personal/information.html")


# 跳转到我的收藏
def to_collection(request):
    return render(request, "personal/collection.html")


# 跳转到我的消息
def to_feedback(request):
    return render(request, "personal/feedback.html")

